//
//  BaseViewController.swift
//  JXPagingView
//
//  Created by jiaxin on 2018/8/10.
//  Copyright © 2018年 jiaxin. All rights reserved.
//

import UIKit
import JXPagingView
import JXSegmentedView
import MTCategory

extension JXPagingListContainerView: JXSegmentedViewListContainer {}

class ClubBaseViewController: BaseViewController {
    lazy var pagingView: JXPagingView = preferredPagingView()
    lazy var userHeaderView: PagingViewTableHeaderView = preferredTableHeaderView()
    let dataSource: JXSegmentedTitleDataSource = JXSegmentedTitleDataSource()
    lazy var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat(headerInSectionHeight)))
    var titles = ["最富俱乐部", "最新俱乐部"]
    var tableHeaderViewHeight: Int = 56
    var headerInSectionHeight: Int = 42
    var isNeedHeader = true
    var isNeedFooter = true

    override func viewDidLoad() {
        super.viewDidLoad()
        userHeaderView.delegate = self
        self.title = "个人中心"
        self.navigationController?.navigationBar.isTranslucent = false

        dataSource.titles = titles
        dataSource.titleSelectedColor = UIColor.text.x1d1a1a
        dataSource.titleNormalColor = UIColor.text.x9b9b9b
        dataSource.isTitleColorGradientEnabled = true
        dataSource.isTitleZoomEnabled = true
        dataSource.isItemSpacingAverageEnabled = false

        segmentedView.backgroundColor = UIColor(hex: 0xf4f4f4)
        segmentedView.delegate = self
        segmentedView.isContentScrollViewClickTransitionAnimationEnabled = false
        segmentedView.dataSource = dataSource

        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = UIColor.text.x1d1a1a
        indicator.indicatorWidth = 20
        indicator.indicatorHeight = 3
        indicator.verticalOffset = 5
        segmentedView.indicators = [indicator]

        pagingView.mainTableView.gestureDelegate = self
        self.view.addSubview(pagingView)
        
        segmentedView.listContainer = pagingView.listContainerView

        //扣边返回处理，下面的代码要加上
        pagingView.listContainerView.contentScrollView().panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.mainTableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.mainTableView.mj_header = ViewFactory.normalHeader {
            let index = self.segmentedView.selectedIndex
            if let d: MTRichClubRateViewController = self.pagingView.validListDict[index] as? MTRichClubRateViewController {
                if index == 0 {
                    d.loadFirst()
                } else {
                    d.loadFirst()
                }
            }
            //self.pagingView.mainTableView.mj_header?.endRefreshing()
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (segmentedView.selectedIndex == 0)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        pagingView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NavigationBarHeight + StatusBarHeight)
            make.left.right.bottom.equalToSuperview()
        }
    }

    func preferredTableHeaderView() -> PagingViewTableHeaderView {
        return PagingViewTableHeaderView(title: "输入俱乐部名称")
    }

    func preferredPagingView() -> JXPagingView {
        return JXPagingView(delegate: self)
    }
}

extension ClubBaseViewController: JXPagingViewDelegate {

    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return tableHeaderViewHeight
    }

    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return userHeaderView
    }

    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return headerInSectionHeight
    }

    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentedView
    }

    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return titles.count
    }

    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        var richClub: MTRichClubRateViewController?
        if index == 0 {
            richClub = MTRichClubRateViewController(clubType: 0) {
                pagingView.mainTableView.mj_header?.endRefreshing()
            }
        } else {
            richClub = MTRichClubRateViewController(clubType: 1) {
                pagingView.mainTableView.mj_header?.endRefreshing()
            }
        }
        
        return richClub!
    }
}

extension ClubBaseViewController: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (index == 0)
    }
}

extension ClubBaseViewController: JXPagingMainTableViewGestureDelegate {
    func mainTableViewGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        //禁止segmentedView左右滑动的时候，上下和左右都可以滚动
        if otherGestureRecognizer == segmentedView.collectionView.panGestureRecognizer {
            return false
        }
        return gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) && otherGestureRecognizer.isKind(of: UIPanGestureRecognizer.self)
    }
}


extension ClubBaseViewController: PagingViewTableHeaderViewDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) {
        let searchVC: MTSearchClubResultViewController = MTSearchClubResultViewController(selectedIndex: self.segmentedView.selectedIndex)
        //navigationController?.pushViewController(MTSearchAddFriendViewController(), animated: true)
        let nav = BaseNavController(rootViewController: searchVC)
        nav.modalPresentationStyle = .fullScreen
        nav.modalTransitionStyle = .crossDissolve
        self.navigationVC?.present(nav, animated: true, completion: nil)
    }
}
